package com.adapter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.adapter.VideoListAdapter.GetCSDNLogoTask;
import com.adapter.VideoListAdapter.ViewHolder;
import com.entity.FileInfo;
import com.entity.Navigation;
import com.videoplayer.R;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DownloadAdapter extends BaseAdapter {

	private List<FileInfo> list = new ArrayList<FileInfo>();
	private ViewHolder viewHolder;
	private Context c;
	private String flag;
	private java.text.DecimalFormat df;

	public DownloadAdapter(Context c, List<FileInfo> list) {
		this.c = c;
		this.list = list;
		df = new java.text.DecimalFormat("#.##");
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public FileInfo getItem(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	public void setFlag(String i) {
		flag = i;
	}

	@Override
	public View getView(int arg0, View convertView, ViewGroup arg2) {

		if (convertView == null) {
			convertView = View.inflate(c, R.layout.down_listview_item, null);
			viewHolder = new ViewHolder();
			viewHolder.title = (TextView) convertView
					.findViewById(R.id.tv_down_title);
			viewHolder.size = (TextView) convertView
					.findViewById(R.id.tv_down_size);
			viewHolder.time = (TextView) convertView
					.findViewById(R.id.tv_down_time);
			viewHolder.ivSing = (ImageView) convertView
					.findViewById(R.id.iv_down);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if (list != null) {
			long time = list.get(arg0).motifyTime;
			Calendar cal = Calendar.getInstance();
			cal.setTimeInMillis(time);
			viewHolder.title.setText(list.get(arg0).Name);

			viewHolder.size.setText(df.format(list.get(arg0).Size) + "Mb");
			viewHolder.time.setText(cal.getTime().toLocaleString());
			if (flag != null) {
				String temp = flag.replaceAll("[\\p{Punct}\\p{Space}]+", "");
				if (temp.equals(viewHolder.title.getText())) {
//					viewHolder.title.setTextColor(0xFF00d0fd);
					viewHolder.ivSing.setBackgroundResource(R.drawable.item_download);
				} else {
//					viewHolder.title.setTextColor(0xFF343434);
					viewHolder.ivSing.setBackgroundResource(R.drawable.item_download_complete);
				}
			}

		}
		return convertView;
	}

	static class ViewHolder {
		private TextView title;
		private TextView size;
		private TextView time;
		private ImageView ivSing;
	}

}
